//
//  BIDArcMenuItem.m
//  EducationAssistant
//
//  Created by mal on 15/7/21.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDArcMenuItem.h"
#import "BIDGlobalMethods.h"

const int kArcMenuItemFontSize = 11;
const int kArcMenuItemImgViewWidth = 40;
const int kArcMenuItemImgViewHeight = 40;
const int kArcMenuItemLabelHeight = 10;
const int kArcMenuItemSpacing = 2;

@interface BIDArcMenuItem()
{
}
@end

@implementation BIDArcMenuItem
@synthesize imgView;
@synthesize titleLabel;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithTitle:(NSString *)title img:(NSString *)imgName pos:(CGPoint)pt
{
    self = [super init];
    if(self)
    {
        int ownWidth = 0;
        int ownHeight = kArcMenuItemImgViewHeight + kArcMenuItemSpacing + kArcMenuItemLabelHeight;
        int labelWidth = [BIDGlobalMethods getWidthWithString:title font:[UIFont systemFontOfSize:kArcMenuItemFontSize] constraintSize:CGSizeMake(MAXFLOAT, kArcMenuItemLabelHeight)];
        ownWidth = labelWidth>kArcMenuItemImgViewWidth?labelWidth:kArcMenuItemImgViewWidth;
        self.frame = CGRectMake(pt.x-kArcMenuItemImgViewWidth/2, pt.y-kArcMenuItemImgViewHeight/2, ownWidth, ownHeight);
        //
        NSArray *arr = [imgName componentsSeparatedByString:@"."];
        NSString *strNormalImgName = [[NSString alloc] initWithFormat:@"%@_normal.%@", arr[0], arr[1]];
        NSString *strHighlightedImgName = [[NSString alloc] initWithFormat:@"%@_highlight.%@", arr[0], arr[1]];
        imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:strNormalImgName] highlightedImage:[UIImage imageNamed:strHighlightedImgName]];
        imgView.frame = CGRectMake((ownWidth-kArcMenuItemImgViewWidth)/2, 0, kArcMenuItemImgViewWidth, kArcMenuItemImgViewHeight);
        imgView.clipsToBounds = YES;
        imgView.layer.cornerRadius = kArcMenuItemImgViewHeight/2;
        [self addSubview:imgView];
        //
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((ownWidth-labelWidth)/2, CGRectGetMaxY(imgView.frame)+kArcMenuItemSpacing, labelWidth, kArcMenuItemLabelHeight)];
        titleLabel.font = [UIFont systemFontOfSize:kArcMenuItemFontSize];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = title;
        [self addSubview:titleLabel];
    }
    return self;
}

+ (int)ArcMenuItemRadius
{
    return kArcMenuItemImgViewHeight/2;
}

@end
